<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="../css/loginstyle.css">
    <title>忘记密码</title>
    <!-- 引入vue框架 -->
    <script src="../lib/js/vue.js"></script>
    <!-- 引入axios库 -->
    <script src="../lib/js/axios.js"></script>
    <!-- 引入config -->
    <script src="../lib/js/config.js"></script>
</head>
<body>
    <div class="login-card" id="forgetPassword-form">
        <h2>忘记密码</h2>
        <h3>Enter your credentials</h3>
        <form class="login-form">
            <input type="text" placeholder="用户名" v-model="user.userAccount" required>
            <input type="email" placeholder="邮箱" v-model="user.email" required>
           
            <button type="button" @click="forgetPassword">重置密码</button>
            <button type="button" @click="onCancel">取消</button>
        </form>
    </div>
    <script>
        var forgetPassword = new Vue({
            el: '#forgetPassword-form',
            data: {
                user: {
                    userAccount: "",
                    email: "",
                }
            },
            methods: {
                // 忘记密码
                forgetPassword() {
                    if (this.user.userAccount === "" || this.user.email === "") {
                        alert("用户名和邮箱不能为空");
                        return;
                    }

                    var url = '/user/forgotPassword/${this.user.userAccount}';
                    var data = {
                        email: this.user.email,
                    };

                    axios.post(url, data)
                        .then(function (response) {
                            console.log(response);
                            if (response.data.data == true) {
                                // console.log("重置成功,请回你的邮箱查看重置密码");
                                // alert("1");
                                alert("重置成功,请回你的邮箱查看重置密码");
                                window.location.href = "../html/login.html";
                            } else {
                                alert(response.data.message);
                            }
                        })
                        .catch(function (error) {
                            console.error('重置密码失败:', error);
                            alert('重置密码失败，请检查用户名和邮箱!');
                        });
                },
                onCancel() {
                    window.location.href = "../html/login.html";
                }
            }
        });
    </script>
</body>
</html>